//
//  CCKMeTableViewController.m
//  budejieMe
//
//  Created by SuperJJ on 16/8/25.
//  Copyright © 2016年 SuperJJ. All rights reserved.
//

#import "CCKMeTableViewController.h"
#import "CCKPublishView.h"
#import "CCKSubscribeTableViewCell.h"
#import "CCKSubscibeTitleTableViewCell.h"
#import "CCKSubscribeModel.h"
#import "CCKPublishCell.h"
#import "CCKPubMoreTableViewController.h"
#import "CCKWebViewController.h"
#import "CCKEnshrineTableViewController.h"
#import "CCKSubMainController.h"
#import "MineUrlDefine.h"


static NSString * const kTableViewCellRidSubscribe = @"kTableViewCellRidSubscribe";
static NSString * const kTableViewCellRidSubscribeTitle = @"kTableViewCellRidSubscribeTitle";


typedef enum
{
    MeCellTypeUser = 0,
    MeCellTypePublish,
    MeCellTypeSubscribe,
}MeCellType;

@interface CCKMeTableViewController () <CCKPublishViewDelegate>

@property (nonatomic, strong) NSArray <CCKSubscribeModel *> *subscribeModelArray;

@end

@implementation CCKMeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavItem];

    [self steupTableView];
}

-(instancetype)init
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(NSArray<CCKSubscribeModel *> *)subscribeModelArray
{
    if (!_subscribeModelArray)
    {
        [CCKSubscribeModel downloadDataModelWithUrl:kSubscribeUrl andSuccessBlock:^(NSArray *array)
        {
            _subscribeModelArray = array;
            
            [self.tableView reloadData];
            
        } andFailedBlock:^(NSError *error)
        {
            NSLog(@"%s Error:%@", __FUNCTION__, error);
        }];
    }
    return _subscribeModelArray;
}

-(void)steupTableView
{
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.tableView registerClass:[CCKSubscribeTableViewCell class] forCellReuseIdentifier:kTableViewCellRidSubscribe];
    [self.tableView registerClass:[CCKSubscibeTitleTableViewCell class] forCellReuseIdentifier:kTableViewCellRidSubscribeTitle];
}

-(void)initNavItem
{
    UIBarButtonItem *settingItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mine-setting-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:nil];
    
    UIBarButtonItem *moonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mine-moon-icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:nil];
    
    self.navigationItem.rightBarButtonItems = @[settingItem, moonItem];
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == MeCellTypeSubscribe)
    {
        return self.subscribeModelArray.count + 1;
    }

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == MeCellTypeUser) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        cell.imageView.image = [UIImage imageNamed:@"default_header_image_small_32x32_"];
        cell.textLabel.text = @"登录/注册";
        
        return cell;
    
    } else if (indexPath.section == MeCellTypePublish) {
        
        CCKPublishView *publishView = [tableView dequeueReusableCellWithIdentifier:PUBLISH_VIEW];
        publishView.delegate = self;
        
        if (!publishView) {
            
            publishView = [[CCKPublishView alloc] init];
        }
        
        return publishView;
     
    } else {
        
        if (indexPath.row == 0)
        {
            CCKSubscibeTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellRidSubscribeTitle forIndexPath:indexPath];
            return cell;
        }else
        {
            CCKSubscribeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellRidSubscribe forIndexPath:indexPath];
            
            cell.model = self.subscribeModelArray[indexPath.row - 1];
            
            return cell;
        }

    }
    
    return nil;
}

// 返回组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

//返回组尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001; //如果返回0会默认15,所以取小的值
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == MeCellTypeSubscribe)
    {
        if (indexPath.row == 0)
        {
            return 25;
        }else
        {
            return 56;
        }
        
    }else if(indexPath.section == MeCellTypePublish)
    {
        return 170;
    }else
    {
        return 50;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    [tableView deselectRowAtIndexPath:indexPath animated:indexPath];
    
    
    if(indexPath.section == MeCellTypeSubscribe)
    {
        CCKSubMainController *subCategoryController = [[CCKSubMainController alloc] initWithStyle:UITableViewStylePlain];
        
        subCategoryController.subscribeModel = self.subscribeModelArray[indexPath.row - 1];
        
        [self.navigationController pushViewController:subCategoryController animated:YES];
    }
}

#pragma mark - CCKPublishViewDelegate

- (void)publishViewGetClickedWith:(CCKPublishView *)publishView andIndexPath:(NSIndexPath *)indexPath
{
    CCKPublishCell *selectedCell = (CCKPublishCell *)[publishView.publishCollectionView cellForItemAtIndexPath:indexPath];
    
    NSString *urlStr = selectedCell.pubLabel.url;
    NSString *iconName = selectedCell.pubLabel.text;
 
    if ([iconName isEqualToString:@"更多"]) {
        
        CCKPubMoreTableViewController *publishMoreVC = [CCKPubMoreTableViewController new];
        publishMoreVC.urlStr = PUBLISH_MORE_URL;
        
        self.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:publishMoreVC animated:YES];
    }
    
    if ([urlStr hasPrefix:@"http://"] || [urlStr hasPrefix:@"toutiao.eastday.com"]) {
        
        //self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(doNothing)];
        
        CCKWebViewController *webVC = [CCKWebViewController new];
        webVC.urlStr = urlStr;
        
        [self.navigationController pushViewController:webVC animated:YES];
        
    } else if ([iconName isEqualToString:@"我的收藏"]) {
        
        CCKEnshrineTableViewController *enshrineVC = [CCKEnshrineTableViewController new];
        
        self.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:enshrineVC animated:YES];
    }
    
}

- (void)doNothing
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
